package com.masterclient.app.modules.brand.carefully;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.utils.status.StatusBarUtil;
import com.eoner.baselibrary.bean.brand.BrandCarefullyInfo;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.brand.carefully.adapter.BrandCarefullyAdapter;
import com.masterclient.app.widget.TitlebarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * created by wujin on 2021/5/2
 */
@Route(path = AroutePath.BRAND_CAREFULLY_ACTIVITY)
public class BrandCarefullyActivity extends BaseActivity<BrandCarefullyPresenter> implements BrandCarefullyContract.View {

    @BindView(R.id.title_view)
    TitlebarView title_view;

    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BrandCarefullyAdapter adapter;

    private boolean hasMore = true;
    private List<BrandCarefullyInfo> data;
    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand_carefully);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));
        addListener();
        init();
        mPresenter.requestData(page);
    }

    private void init() {
        adapter = new BrandCarefullyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (hasMore) {
                    mPresenter.requestData(page);
                }
            }
        }, recyclerView);
    }

    private void refresh() {
        if (data != null) {
            data.clear();
        }
        page = 1;
        mPresenter.requestData(page);
    }

    private void addListener() {
        title_view.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @Override
    public void loadSuccess(List<BrandCarefullyInfo> list) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        hasMore = list != null && list.size() > 0;
        if (data == null) {
            data = new ArrayList<>();
        }
        data.addAll(list);
        adapter.setNewData(data);
        if (hasMore) {
            page++;
        } else {
            adapter.setEnableLoadMore(false);
        }
        adapter.notifyDataSetChanged();
    }

}
